package com.CusterRelSys.service.impl;

import com.CusterRelSys.entity.Page;
import com.CusterRelSys.entity.SalChance;
import com.CusterRelSys.entity.SysUser;
import com.CusterRelSys.mapper.SalChanceZjjMapper;
import com.CusterRelSys.service.ISalChanceZjjService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 张俊杰 on 2017/12/1.
 */
@Service("salChanceZjjService")
public class SalChanceZjjServiceImpl implements ISalChanceZjjService {

    @Autowired
    SalChanceZjjMapper mapper;
    /**
     * 统计查询的总页数
     *
     * @param page
     * @return
     */
    @Override
    public int countPage(Page<SalChance> page) {
        return mapper.countPage(page);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    @Override
    public List<SalChance> selectSalChanceByPage(Page<SalChance> page) {
        return mapper.selectSalChanceByPage(page);
    }

    /**
     * 删除
     *
     * @param salChance
     */
    @Override
    public void deleteSalChanceById(SalChance salChance) {
        mapper.deleteSalChanceById(salChance);
    }

    /**
     * 根据id获取值
     *
     * @param integer
     * @return
     */
    @Override
    public SalChance selectSalChanceById(Integer integer) {
        return mapper.selectSalChanceById(integer);
    }

    /**
     * 添加
     *
     * @param salChance
     */
    @Override
    public boolean insertSalChance(SalChance salChance) {
        return mapper.insertSalChance(salChance);
    }

    /**
     * 查询用户表的所有记录
     *
     * @return
     */
    @Override
    public List<SysUser> selectSysUserAll() {
        return mapper.selectSysUserAll();
    }

    /**
     * 指派销售机会
     *
     * @param salChance
     * @param chc_DueDate
     * @return
     */
    @Override
    public boolean updateSalChanceByIdDispatch(SalChance salChance, String chc_DueDate) {

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(chc_DueDate);
            salChance.setChcDueDate(date.getTime());
            boolean b = mapper.updateSalChanceByIdDispatch(salChance);
            return b;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 编辑销售机会
     *
     * @param salChance
     * @param chc_DueDate
     * @param chc_CreateDate @return
     */
    @Override
    public boolean updateSalChanceByIdEdit(SalChance salChance, String chc_DueDate, String chc_CreateDate) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = sdf.parse(chc_DueDate);
            salChance.setChcDueDate(date.getTime());
            if(StringUtils.isNotEmpty(chc_CreateDate)){
                date = sdf.parse(chc_CreateDate);
                salChance.setChcCreateDate(date.getTime());
            }
            System.out.println(salChance);
            boolean b = mapper.updateSalChanceByIdEdit(salChance);
            return b;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
}
